//
//  AboutUserViewController.m
//  Light
//
//  Created by 骁唐 on 15/11/2.
//  Copyright © 2015年 骁唐. All rights reserved.
//

#import "AboutUserViewController.h"
#import "WXApi.h"
#import "AboutUserViewCellLabel.h"
#import "AboutUserViewCellButton.h"

@interface AboutUserViewController ()<UIGestureRecognizerDelegate,UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>
//背景图片
@property (weak, nonatomic) IBOutlet UIImageView *backGround;
//tableView
@property (weak, nonatomic) IBOutlet UITableView * tableView;

@end

@implementation AboutUserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [self configTableViewHeadView];//配置logo
}

#pragma mark -
#pragma mark 配置headView
- (void)configTableViewHeadView
{
    [self.tableView setBackgroundColor:[UIColor clearColor]];
     self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    UIView * viewLogo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
    

    UIImage * imageLogo = [UIImage imageNamed:@"logo"];
    UIImageView * imageViewLogo = [[UIImageView alloc] initWithImage:imageLogo];

    CGRect rect = imageViewLogo.frame;
    rect.origin.x = ((int)(SCREEN_WIDTH - imageViewLogo.frame.size.width))/2;
    rect.origin.y = ((int)(200 - imageViewLogo.frame.size.height))/2-15;
    imageViewLogo.frame = rect;

    //自适应图片宽高比例
    imageViewLogo.contentMode = UIViewContentModeScaleAspectFit;
    [viewLogo addSubview:imageViewLogo];
    [viewLogo setBackgroundColor:[UIColor clearColor]];
    
    [self.tableView setTableHeaderView:viewLogo];//设置头部视图view
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of wany resources that can be recreated.
}

#pragma mark -
#pragma mark 滑动协议方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return  YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}

- (void)viewDidAppear:(BOOL)animated {
    //禁用滑动手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -
#pragma mark 导航栏返回
- (IBAction)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //强制竖屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:
                                        UIInterfaceOrientationPortrait]  forKey:@"orientation"];
    long long int currentTimeInt = [TJJDefines getTimeIntSince1970Current];    
    if (currentTimeInt>=16200&& currentTimeInt <=34200) {
        self.backGround.image = [UIImage imageNamed:@"backgroundOne"];
    }else if (currentTimeInt>34200 && currentTimeInt<=55800)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundTwo"];
    }else if (currentTimeInt>55800 && currentTimeInt<=70200)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundThree"];
    }else if(currentTimeInt >70200 || currentTimeInt<16200)
    {
        self.backGround.image = [UIImage imageNamed:@"background_night"];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (alertView.tag) {
        case 98:
        {//点击进入微信
            switch (buttonIndex) {
                case 0:
                {//取消
                }
                    break;
                case 1:
                {//登录微信
                    if ([WXApi isWXAppInstalled])
                    {
                        NSString *str = @"weixin://qr";
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                    }
                    else
                    {
                        TJJALERT_VIEW(@"您还没有安装微信！");
                    }
                }
                    break;
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section)
    {
        case 0:
        {
            return 30.0;//标签展示cell
        }
            break;
        case 1:
            return 55.0;//button展示cell
            break;
            
        default:
            return 0;
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 4;
    }
    else if (section == 1)
    {
        return 3;
    }
    else
    {
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellLabelID = @"AboutUserViewCellLabel";
    NSString *cellButtonID = @"AboutUserViewCellButton";
    switch (indexPath.section)
    {
        case 0:
        {//第一部分
            AboutUserViewCellLabel * cell = [tableView dequeueReusableCellWithIdentifier:cellLabelID];
            if (!cell) {
                cell =[[[NSBundle mainBundle]loadNibNamed:cellLabelID owner:nil options:nil] lastObject];
            }
             UserConfig *config = [UserConfig sharingUserConfig];//用户信息
//             NSString *cool = [config getConnectCool];
             AppDelegate * delegate = [[UIApplication sharedApplication] delegate];
            //设置展示内容：
            switch (indexPath.row)
            {
                case 0:
                {//应用版本
                    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];//app应用字典信息
                    NSString * app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];// app版本
                    cell.labelTitle.text = [NSString stringWithFormat:@"应用版本：%@",app_Version];//应用版本;
                }
                    break;
                case 1:
                {//固件版本号
                    if (delegate.Bluetooth)
                    {//如果蓝牙 连接
                        if ([TJJDefines isNull:[config getHardware]])
                        {//如果固件版本没有读取到
                            cell.labelTitle.text = @"固件版本号：不可用";//固件序列号版本
                        }
                        else
                        {
                            NSString * stringHardwareVersion = [config getHardware];//固件序列号版本
                            cell.labelTitle.text = [NSString stringWithFormat:@"固件版本号：%@",[stringHardwareVersion uppercaseString]];//固件序列号版本
                        }
                    }
                    else
                    {
                        cell.labelTitle.text = @"固件版本号：不可用";
                    }
                }
                    break;
                case 2:
                {//蓝牙适配器版本：
                    if (delegate.Bluetooth)
                    {//如果蓝牙 连接
                        if ([TJJDefines isNull:[config getBLEHardware]])
                        {//如果固件版本没有读取到
                            cell.labelTitle.text = @"蓝牙适配器版本：不可用";//固件序列号版本
                        }
                        else
                        {
                            NSString * stringBLEHardwareVersion = [config getBLEHardware];
                            cell.labelTitle.text = [NSString stringWithFormat:@"蓝牙适配器版本：%@",[stringBLEHardwareVersion uppercaseString]];//固件序列号版本
                        }
                    }
                    else
                    {
                        cell.labelTitle.text = @"蓝牙适配器版本：不可用";
                    }
                }
                    break;
                case 3:
                {//序列号
                    //                    if ([cool intValue] == 1 && self)
                    if (delegate.Bluetooth)
                    {//如果蓝牙 连接
                        if ([TJJDefines isNull:[config getSerialNumber]])
                        {//如果序列号没有读取到
                            cell.labelTitle.text = @"产品序列号：不可用";//固件序列号版本
                        }
                        else
                        {
                            NSString * stringSerialNumber = [config getSerialNumber];
                            cell.labelTitle.text = [NSString stringWithFormat:@"产品序列号：%@",[stringSerialNumber uppercaseString]];//固件序列号版本
                        }
                    }
                    else
                    {
                        cell.labelTitle.text = @"产品序列号：不可用";
                    }
                }
                    break;
                default:
                    break;
            }
            [cell setBackgroundColor:[UIColor clearColor]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 1:
        {//第二部分
            AboutUserViewCellButton * cell = [tableView dequeueReusableCellWithIdentifier:cellButtonID];
            if (!cell)
            {
                cell =[[[NSBundle mainBundle]loadNibNamed:cellButtonID owner:nil options:nil] lastObject];
            }
            switch (indexPath.row)
            {
                case 0:
                {//微信平台
                    cell.labelLabel.text = @"微信平台";
                }
                    break;
                case 1:
                {//访问官网
                    cell.labelLabel.text = @"访问官网";
                }
                    break;
                case 2:
                {//协议与隐私
                    cell.labelLabel.text = @"协议与隐私";
                }
                    break;
                    
                default:
                    break;
            }
            [cell setBackgroundColor:[UIColor clearColor]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
            
        default:
            break;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1)
    {
        switch (indexPath.row)
        {
            case 0:
            {//微信平台
                if (TJJiOS8) {
                    UIAlertController  * Alertview =  [UIAlertController alertControllerWithTitle:@"神灯科技微信平台：magilit" message:nil preferredStyle:UIAlertControllerStyleAlert];
                    
                    [self presentViewController:Alertview animated:YES completion:nil];
                    
                    UIAlertAction *action = [UIAlertAction actionWithTitle:@"打开微信" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        if ([WXApi isWXAppInstalled]) {
                            NSString *str = @"weixin://qr";
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                        }else
                        {
                            TJJALERT_VIEW(@"您还没有安装微信！");
                        }
                    }];
                    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    }];
                    [Alertview addAction:action];
                    [Alertview addAction:action2];
                }
                else
                {
                    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"神灯科技微信平台：magilit" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"打开微信", nil];
                    alert.tag = 98;
                    [alert show];
                }
            }
                break;
            case 1:
            {//访问官网
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.magilit.com"]];
            }
                break;
            case 2:
            {//协议与隐私
                [self performSegueWithIdentifier:@"aboutUserToProtocol" sender:self];
            }
                break;
            default:
                break;
        }
    }
}

@end
